<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 15/8/9
 * Time: 下午11:53
 */
class ModGamePayment
{
	
	public function getData($params){
		$where = ' WHERE RecDate BETWEEN :t_s AND :t_e
                        AND Period=:period
                        AND GameSrc=:g ';
	
		$args = array(
				':t_s'    => $params['time_start'],
				':t_e'    => $params['time_end'],
				':g'      => empty($params['g']) ? 'ALL' : $params['g'],
				':period' => 'Daily'
		);
		
		$user = AccAuth::me();
		if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
			$where .= " AND Platform=:p ";
			$args[':p'] = $params['os'];
		}
		
		if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
			$modServer = new ModServer();
			$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
			$where .= " AND ( ";
			foreach ($list as $key=>$val){
				$where .= " SID = '{$val['develop_id']}'  or";
			}
			$where = substr($where, 0, -2);
			$where .= " ) ";
		}else{
			$where .= " AND SID=:ser ";
			$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
		}
	
		$table = 'tblGamePayment';
	
		$sql = "SELECT * FROM {$table} {$where}";
	
		$dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
		$res = $dbLink->query($sql, $args);
	
		return $res;
	}
	
	
	
    public function getData1($params){
        $where = ' WHERE RecDate BETWEEN :t_s AND :t_e
                        AND Period=:period
                        AND Platform=:p
                        AND GameSrc=:g
                        AND SID=:ser';

        $args = array(
            ':t_s'    => $params['time_start'],
            ':t_e'    => $params['time_end'],
            ':g'      => empty($params['g']) ? 'ALL' : $params['g'],
            ':p'      => $params['os'],
            ':ser'    => empty($params['server']) ? 'ALL' : $params['server'],
            ':period' => 'Daily'
        );

        $table = 'tblGamePayment';

        $sql = "SELECT * FROM {$table} {$where}";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
        $res = $dbLink->query($sql, $args);

        return $res;
    }
     
    
    public function getGradeData($params){
    	$where = ' WHERE RecDate BETWEEN :t_s AND :t_e
                        AND Period=:period
                        AND GameSrc=:g';
    	
    	$args = array(
    			':t_s'    => $params['time_start'],
    			':t_e'    => $params['time_end'],
    			':g'      => empty($params['g']) ? 'ALL' : $params['g'],
    			':period' => 'Daily'
    	);
    	
    	$user = AccAuth::me();
    	if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
    		$where .= " AND Platform=:p ";
    		$args[':p'] = $params['os'];
    	}
    	
    	if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
    		$modServer = new ModServer();
    		$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
    		$where .= " AND ( ";
    		foreach ($list as $key=>$val){
    			$where .= " SID = '{$val['develop_id']}'  or";
    		}
    		$where = substr($where, 0, -2);
    		$where .= " ) ";
    		
    		$field = ' GameSrc,Level,SUM(PayUser) AS PayUser, SUM(PayCount) AS PayCount, SUM(PayMoney) AS PayMoney, SUM(PayGold) AS PayGold ';
    		$group = ' GROUP BY GameSrc,Level ';	
    	}else{
    		$where .= " AND SID=:ser ";
    		$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
    		
    		$field = ' GameSrc,SID,Level,SUM(PayUser) AS PayUser, SUM(PayCount) AS PayCount, SUM(PayMoney) AS PayMoney, SUM(PayGold) AS PayGold ';
    		$group = 'GROUP BY GameSrc,SID,Level';
    	}
        $table = 'tblgamepaylevel';

    	$sql = "SELECT {$field} FROM {$table} {$where} {$group}";

    	$dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
    	$res = $dbLink->query($sql, $args);
    	
    	return $res;
    }

    public function getLtvData($params){
        $where = ' WHERE RecDate BETWEEN :t_s AND :t_e
                        AND Period=:period
                        AND GameSrc=:g ';

        $args = array(
            ':t_s'    => $params['time_start'],
            ':t_e'    => $params['time_end'],
            ':g'      => empty($params['g']) ? 'ALL' : $params['g'],
            ':period' => 'Daily'
        );
        
        $user = AccAuth::me();
        if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
        	$where .= " AND Platform=:p ";
        	$args[':p'] = $params['os'];
        }
         
        if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
        	$modServer = new ModServer();
        	$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
        	$where .= " AND ( ";
        	foreach ($list as $key=>$val){
        		$where .= " SID = '{$val['develop_id']}'  or";
        	}
        	$where = substr($where, 0, -2);
        	$where .= " ) ";
        
        	$field = ' GameSrc,Level,SUM(PayUser) AS PayUser, SUM(PayCount) AS PayCount, SUM(PayMoney) AS PayMoney, SUM(PayGold) AS PayGold ';
        	$group = ' GROUP BY GameSrc,Level ';
        }else{
        	$where .= " AND SID=:ser ";
        	$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
        
        	$field = ' GameSrc,SID,Level,SUM(PayUser) AS PayUser, SUM(PayCount) AS PayCount, SUM(PayMoney) AS PayMoney, SUM(PayGold) AS PayGold ';
        	$group = 'GROUP BY GameSrc,SID,Level';
        }

        $table = 'tblPaymentLTV';

        $sql = "SELECT * FROM {$table} {$where}";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
        $res = $dbLink->query($sql, $args);
        return $res;
    }

    public function getUserData($params){
        $where = ' WHERE a.RecDate BETWEEN :t_s AND :t_e
                        AND a.Period=:period
                        AND a.Platform=:p
                        AND a.GameSrc=:g
                        AND a.SID=:ser';

        $args = array(
            ':t_s'    => $params['time_start'],
            ':t_e'    => $params['time_end'],
            ':g'      => empty($params['g']) ? 'ALL' : $params['g'],
            ':p'      => $params['os'],
            ':ser'    => empty($params['server']) ? 'ALL' : $params['server'],
            ':period' => 'Daily'
        );

        $sql = "SELECT a.*,b.DAU FROM tblGamePayment a
                LEFT JOIN tblGameLogin b ON b.RecDate=a.RecDate AND b.Period=a.Period
                    AND b.Platform=a.Platform AND b.GameSrc=a.GameSrc AND b.SID=a.SID
                {$where}";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
        $res = $dbLink->query($sql, $args);
        return $res;
    }


    public function getRetainData($params){
        $where = ' WHERE RecDate BETWEEN :t_s AND :t_e
                        AND Period=:period
                        AND Platform=:p
                        AND GameSrc=:g
                        AND SID=:ser';

        $args = array(
            ':t_s'    => $params['time_start'],
            ':t_e'    => $params['time_end'],
            ':g'      => empty($params['g']) ? 'ALL' : $params['g'],
            ':p'      => $params['os'],
            ':ser'    => empty($params['server']) ? 'ALL' : $params['server'],
            ':period' => 'Daily'
        );

        $table = 'tblGamePayRemain';

        $sql = "SELECT * FROM {$table} {$where}";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
        $res = $dbLink->query($sql, $args);
        return $res;
    }
    
    
    public function getIntervalData($params){
    	$where = ' WHERE RecDate BETWEEN :t_s AND :t_e
                        AND Period=:period
                        AND GameSrc=:g ';
    	
    	$args = array(
    			':t_s'    => $params['time_start'],
    			':t_e'    => $params['time_end'],
    			':g'      => empty($params['g']) ? 'ALL' : $params['g'],
    			':period' => 'Daily'
    	);
    	
    	$user = AccAuth::me();
    	if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
    		$where .= " AND Platform=:p ";
    		$args[':p'] = $params['os'];
    	}
    	
    	if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
    		$modServer = new ModServer();
    		$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
    		$where .= " AND ( ";
    		foreach ($list as $key=>$val){
    			$where .= " SID = '{$val['develop_id']}'  or";
    		}
    		$where = substr($where, 0, -2);
    		$where .= " ) ";
    	}else{
    		$where .= " AND SID=:ser ";
    		$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
    	}
    
    	$table = 'tblgamepayinterval';
    
    	$sql = "SELECT * FROM {$table} {$where}";

    	$dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
    	$res = $dbLink->query($sql, $args);
    	return $res;
    }
}